﻿package com.karst.flab3d
{
	import flash.display.Sprite;
	import flash.events.Event;
	
	import org.papervision3d.cameras.Camera3D;
	import org.papervision3d.materials.BitmapFileMaterial;
	import org.papervision3d.objects.primitives.Sphere;
	import org.papervision3d.render.BasicRenderEngine;
	import org.papervision3d.scenes.Scene3D;
	import org.papervision3d.view.Viewport3D;

	public class Basic3dSample extends Sprite 
	{
		private var viewport : Viewport3D = null ;
		private var renderer : BasicRenderEngine = null ;

		private var scene : Scene3D = null ;
		private var camera : Camera3D = null ;

		private var sphere:Sphere = null ;

		public function Basic3dSample():void 
		{
			super() ;
			createView() ;
			addEvents() ;
		}

		private function createView() : void
		{
			if( viewport == null )
				viewport = new Viewport3D(400,400);
			if( renderer == null )
				renderer = new BasicRenderEngine()  ;
			if( scene == null )
				scene = new Scene3D()  ;
			if( camera == null )
				camera = new Camera3D()  ;
			if( sphere == null )
				sphere = new Sphere(new BitmapFileMaterial('map.jpg'), 200, 12, 12 ) ;

			addChild( viewport ) ;
			scene.addChild( sphere ) ;
		}

		private function addEvents() : void
		{
			addEventListener( Event.ENTER_FRAME, rendHandler ) ;
		}
		
		private function rendHandler( event : Event) : void 
		{
			if( sphere )
				sphere.rotationY++ ;
			renderer.renderScene(scene,camera,viewport);
		}

	}
}